package edu.cmu.cs.webapp.ioms.ui.dialogs.order;

import java.text.NumberFormat;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.Ingredient;
import edu.cmu.cs.webapp.ioms.utils.IOMSUtils;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

public class InventoryRechargeDialog extends TitleAreaDialog {

	private Text _ingTxt;
	private Text _currentInventoryTxt;
	private Text _newPurchaseAmountTxt;
	private Ingredient _model;

	public InventoryRechargeDialog(Shell parentShell, Ingredient model) {
		super(parentShell);
		this._model = model;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Update Inventory");
		setMessage("Update the inventory stock level for '"
				+ this._model.getName() + "'");

		Composite container = SWTUtils.createComposite((Composite) super
				.createDialogArea(parent), 2, SWT.DEFAULT, 1);
		SWTUtils.createLabel(container, "Ingredient:", 1);
		_ingTxt = SWTUtils.createTextBox(container);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		_ingTxt.setLayoutData(gd);
		_ingTxt.setEditable(false);
		_ingTxt.setEnabled(false);

		SWTUtils.createSeparator(container, 2);

		SWTUtils.createLabel(container, "Current Stock Level:", 1);
		_currentInventoryTxt = SWTUtils.createTextBox(container);
		_currentInventoryTxt.setLayoutData(gd);
		_currentInventoryTxt.setEditable(false);

		SWTUtils.createLabel(container, "Refill*:", 1);
		_newPurchaseAmountTxt = SWTUtils.createTextBox(container);
		_newPurchaseAmountTxt.setLayoutData(gd);
		_newPurchaseAmountTxt.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent event) {
				validateInput();
			}
		});

		loadFromExistingModel();
		validateInput();

		return container;
	}

	private void loadFromExistingModel() {
		if (this._model != null) {
			this._ingTxt.setText(IOMSUtils.constructIngText(this._model));
			this._currentInventoryTxt.setText(NumberFormat.getInstance()
					.format(this._model.getInventory()));

		}
	}

	@Override
	protected void buttonPressed(int buttonId) {
		_model.setInventory(getInventory());
		super.buttonPressed(buttonId);
	}

	private double getInventory() {
		double orig = _model.getInventory();
		double amount = 0;

		try {
			amount = Double
					.valueOf(this._newPurchaseAmountTxt.getText().trim());

		} catch (NumberFormatException e) {
			// should not run into here
		}

		return orig + amount;

	}

	public Ingredient getModel() {
		return _model;
	}

	private void enableOKButton(boolean enable) {
		Button okBtn = getButton(IDialogConstants.OK_ID);
		if (okBtn != null) {
			okBtn.setEnabled(enable);
		}
	}

	private void validateInput() {
		boolean valid = true;
		String errorMessage = null;
		if (valid
				&& (this._newPurchaseAmountTxt.getText() == null || this._newPurchaseAmountTxt
						.getText().trim().length() == 0)) {
			valid = false;
			errorMessage = "New purchase amount must be specified!";
		}

		if (valid) {
			try {
				Double.valueOf(this._newPurchaseAmountTxt.getText().trim());
			} catch (NumberFormatException e) {
				valid = false;
				errorMessage = "Invalid input, refill amount must be a number";
			}

		}

		if (!valid && errorMessage != null) {
			setErrorMessage(errorMessage);
			enableOKButton(false);
		} else {
			setErrorMessage(null);
			enableOKButton(true);
		}
	}

}
